package com.zlkj.pro.modular.business.service.impl;

import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlkj.pro.core.util.ConvertUtil;
import com.zlkj.pro.modular.business.entity.BannerEntity;
import com.zlkj.pro.modular.business.mapper.BannerMapper;
import com.zlkj.pro.modular.business.model.dto.BannerDTO;
import com.zlkj.pro.modular.business.model.vo.BannerVO;
import com.zlkj.pro.modular.business.service.BannerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liyang
 * @date 2024/4/6
 * @time 14:34
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class BannerServiceImpl extends ServiceImpl<BannerMapper, BannerEntity> implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Autowired
    private Environment env;

    @Override
    public PageResult<BannerVO> findPage(BannerDTO dto) {
        Integer pageNo = dto.getPageNo();
        Integer pageSize = dto.getPageSize();
        List<BannerVO> list = bannerMapper.findPage(dto);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(vo -> {
                vo.setImageFileUrl(env.getProperty("file-path") + vo.getImageFileId());
            });
        }
        Long total = bannerMapper.findPageCount(dto);
        return PageResultFactory.createPageResult(list, total, pageSize, pageNo);
    }

    @Override
    public List<BannerVO> findList(BannerDTO dto) {
        LambdaQueryWrapper<BannerEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.orderByDesc(BannerEntity::getCreateTime);
        List<BannerEntity> BannerEntityList = super.list(queryWrapper);
        return ConvertUtil.turn(BannerEntityList, BannerVO.class);
    }

    @Override
    public BannerVO findDetail(BannerDTO dto) {
        Long id = dto.getId();
        LambdaQueryWrapper<BannerEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BannerEntity::getId, id);
        BannerEntity BannerEntity = super.getOne(queryWrapper, Boolean.FALSE);
        return ConvertUtil.turn(BannerEntity, BannerVO.class);
    }

    @Override
    public boolean add(BannerDTO dto) {
        BannerEntity entity = ConvertUtil.turn(dto, BannerEntity.class);
        return super.save(entity);
    }

    @Override
    public boolean edit(BannerDTO dto) {
        BannerEntity entity = ConvertUtil.turn(dto, BannerEntity.class);
        return super.updateById(entity);
    }

    @Override
    public boolean delete(BannerDTO dto) {
        return super.removeById(dto);
    }

    @Override
    public boolean batchDelete(List<Long> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            return super.removeByIds(ids);
        }
        return true;
    }

}
